package casts.misc
{
   import flash.display.MovieClip;
   import flash.display.BlendMode;
   import flash.events.Event;
   import flash.geom.Point;
   import flash.geom.Rectangle;

   /**
    * 聚光燈(spotlight)類別，因為通常要營造 spotlight 要用非常巨大的 MovieClip來做，但利用這類別，
    * 現在只需要拉個小小的 spotlight，命名成 mcSpot，程式會避開 mcSpot 填滿其它區塊！
    * @author  cjboy | cjboy1984@gmail.com
    */
   public class Spotlight extends MovieClip
   {
      // fla
      public var mcSpot:MovieClip;

      // 填滿的顏色
      private const COLOR:uint = 0x000000;

      /* constructor */
      public function Spotlight()
      {
         //blendMode = BlendMode.LAYER;

         stop();

         addEventListener(Event.ADDED_TO_STAGE, onAdd);
         addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
      }

      // ------------------ LINE -------------------

      // ################ Protected ################

      // ################# Private #################

      private function onAdd(e:Event):void
      {
         drawBlack();
         addEventListener(Event.ENTER_FRAME, drawBlack);
      }

      private function onRemove(e:Event):void
      {
         removeEventListener(Event.ENTER_FRAME, drawBlack);
      }

      // ------------------ LINE -------------------

      private function drawBlack(e:Event = null):void
      {
         var p:Point = globalToLocal(new Point(0, 0));

         // 分三區畫
         this.graphics.clear();
         this.graphics.beginFill(COLOR);
         // part 1
         this.graphics.drawRect(p.x, p.y, Math.abs(mcSpot.x - p.x), sh);
         // part 2
         this.graphics.drawRect(mcSpot.x, p.y, mcSpot.width, Math.abs(mcSpot.y - p.y));
         // part 3
         this.graphics.drawRect(mcSpot.x, mcSpot.y + mcSpot.height, mcSpot.width, sh - Math.abs(mcSpot.y - p.y) - mcSpot.height);
         // part 4
         this.graphics.drawRect(mcSpot.x + mcSpot.width, p.y, sw - Math.abs(mcSpot.x - p.x) - mcSpot.width, sh);
         this.graphics.endFill();
      }

      /* get stage.stageWidth/Height */
      private function get sw():Number { return stage.stageWidth; }
      private function get sh():Number { return stage.stageHeight; }

      // ------------------ LINE -------------------

   }

}